package com.bright.ghj.overt.web.controller;

import com.bright.ghj.common.pojo.vo.PageVO;
import com.bright.ghj.common.pojo.bo.RestResult;
import com.bright.ghj.overt.util.SecurityUtil;
import com.bright.ghj.common.util.StringUtil;
import com.bright.ghj.overt.pojo.dto.UserDTO;
import com.bright.ghj.overt.pojo.dto.ZtAdaptDTO;
import com.bright.ghj.overt.pojo.dto.ZtDTO;
import com.bright.ghj.overt.pojo.query.DistQuery;
import com.bright.ghj.overt.pojo.vo.DistVO;
import com.bright.ghj.overt.pojo.vo.ZtAdaptVO;
import com.bright.ghj.overt.service.DistService;
import com.bright.ghj.common.util.ListUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author hxj
 * @Date 2023/12/21 11:05
 * @Description 导航项
 */
@RequestMapping("/dist")
@RestController
@RequiredArgsConstructor
@Api(tags = "地区账套接口")
public class DistController {

    private final DistService distService;

//    private final NavigateService navigateService;

    /**
     * 获取当前地区号及下一级地区、账套列表
     * @return
     */
    @ApiOperation(value = "加载地区树")
    @PostMapping("/tree/list")
    public RestResult listDistTrees(@RequestBody DistQuery distQuery) {
        distQuery.setUserDistNo(getUserDistNo());
        distQuery.setUserZtId(getUserZtId());
        List<DistVO> distVOS = distService.listDistTrees(distQuery);
        return RestResult.success(distVOS);
    }

    /**
     * 分页获取账套列表
     * @return
     */
    @ApiOperation(value = "获取账套列表")
    @GetMapping("/zt/page/{distNo}/{pageNo}/{pageSize}")
    public RestResult pageZt(@PathVariable String distNo, @PathVariable String pageNo, @PathVariable String pageSize) {
        DistQuery distQuery = new DistQuery();
        distQuery.setDistNo(distNo);
        distQuery.setPageNumber(Integer.parseInt(pageNo));
        distQuery.setPageSize(Integer.parseInt(pageSize));
        distQuery.setUserDistNo(getUserDistNo());
        distQuery.setUserZtId(getUserZtId());


        PageVO<ZtDTO> ztDTOPageVO = distService.pageZt(distQuery);
        return RestResult.success(ztDTOPageVO);
    }

    /**
     * 不分页获取所有的账套列表 只允许街道级和村级
     * @return
     */
    @ApiOperation(value = "获取账套列表 不分页")
    @GetMapping("/zt/list/{distNo}")
    public RestResult listZt(@PathVariable String distNo) {
        int distNoLength = StringUtil.length(distNo);
        if (distNoLength <= 2) {
            return RestResult.fail("请选择区级及以下的地区！");
        }

        DistQuery distQuery = new DistQuery();
        distQuery.setDistNo(distNo);
        distQuery.setUserDistNo(getUserDistNo());
        distQuery.setUserZtId(getUserZtId());

        List<ZtDTO> ztDTOPageVO = distService.listZt(distQuery);

        // 当传递区级时 数据量过大 导致返回响应过长 缩减所需字段
        List<Map<String, Object>> maps = ListUtil.toSingleList(ztDTOPageVO, ztDTO -> {
            Map<String, Object> map = new HashMap<>();
            map.put("ztId", ztDTO.getZtId());
            map.put("ztName", ztDTO.getZtName());
            map.put("ztJc", ztDTO.getZtJc());
            map.put("ztTypeId", ztDTO.getZtTypeId());
            map.put("ztTypeName", ztDTO.getZtTypeName());
            return map;
        });
        return RestResult.success(maps);
    }

    /**
     * 获取账套适配列表
     * @return
     */
    @ApiOperation(value = "获取账套适配列表")
    @GetMapping("/ztAdapt/page/{distNo}/{pageNo}/{pageSize}")
    public RestResult pageZtAdapt(@PathVariable String distNo, @PathVariable String pageNo, @PathVariable String pageSize) {
        DistQuery distQuery = new DistQuery();
        distQuery.setDistNo(distNo);
        distQuery.setPageNumber(Integer.parseInt(pageNo));
        distQuery.setPageSize(Integer.parseInt(pageSize));
        distQuery.setUserDistNo(getUserDistNo());
        distQuery.setUserZtId(getUserZtId());

        PageVO<ZtAdaptVO> ztAdaptVOS = distService.listZtAdapt(distQuery);
        return RestResult.success(ztAdaptVOS);
    }

    /**
     * 保存账套适配信息
     * @return
     */
    @ApiOperation(value = "保存账套适配信息")
    @PostMapping("/ztAdapt/save")
    public RestResult saveZtAdapt(@RequestBody ZtAdaptDTO ztAdaptDTO) {
        distService.saveZtAdapt(ztAdaptDTO);
        return RestResult.success();
    }

    private List<String> getUserDistNo() {
        // 获取当前登录的用户信息
//        UserDTO loginUser = navigateService.getLoginUser();
        UserDTO loginUser = SecurityUtil.getLoginUser();
        String[] distNoList = loginUser.getDistNoList();
        return distNoList == null ? new ArrayList<>() : new ArrayList<>(Arrays.asList(distNoList));
    }

    private List<String> getUserZtId() {
        // 获取当前登录的用户信息
//        UserDTO loginUser = navigateService.getLoginUser();
        UserDTO loginUser = SecurityUtil.getLoginUser();
        String[] ztIdList = loginUser.getZtIdList();
        return ztIdList == null ? new ArrayList<>() : new ArrayList<>(Arrays.asList(ztIdList));
    }

}
